package java8.lambda.streamAPI;

import javax.swing.text.AbstractDocument;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author vivi20
 * @ClassName StreamAPI
 * @description: TODO
 * @Date 2020/1/19 16:46
 * @Vertion 1.0
 */
public class StreamAPI {



    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 200; i++) {
            list.add("A"+i);
        }
        Stream<String> words = list.stream();
//        Stream<String> longWords = words.filter(i->i.length() > 3);
        Stream<String> lowerCase = words.map(String::toLowerCase);
        Stream<Character> firstChars = words.map(s->s.charAt(0));

        Stream<Stream<Character>> result = words.map(w->characterStream(w));
        //flatMap
        Stream<Character> letter = words.flatMap(w->characterStream(w));
        //随机包含100个随机数字流
        Stream<Double> math = Stream.generate(Math::random).limit(100);
//        Stream<String> skip = Stream.of(contents.split("[\\P{L}]")).skip(1);
        //peek方法会产生另一个与原始流具有相同元素的流，但是在每次获取一个元素时，都会调用一个函数，便于调试。
        Stream<Character> combind = Stream.concat( characterStream("hello"),characterStream("world"));
        Object[] powers = Stream.iterate(1.0,p->p*2).peek(e->System.out.println("Fetching"+e)).limit(20).toArray();

        //去重
        Stream<String> uniqueWords = Stream.of("merrily","merrily","merrily","gently").distinct();
        // 当排序过程只是多个流操作中的一部分时，sorted方法会非常实用
        Stream<String> longestFirst = words.sorted(Comparator.comparing(String::length).reversed());



    }



    private static Stream<Character> characterStream(String s){
        List<Character> characters = new ArrayList<>();
        for (Character o:s.toCharArray()) {
            characters.add(o);
        }
        return characters.stream();
    }

}
